package com.chen;

import com.chen.api.RpcRequest;
import com.chen.api.RpcResolver;
import com.chen.api.RpcResponse;
import com.chen.config.DemoResolver;
import com.chen.server.RpcInvoker;
import com.chen.service.OrderService;
import com.chen.service.UserService;
import com.chen.service.OrderServiceImpl;
import com.chen.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class RpcDemoServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(RpcDemoServerApplication.class,args);
    }

    @Autowired
    RpcInvoker invoker;

    @PostMapping("/")
    public RpcResponse invoke(@RequestBody RpcRequest request){
        System.out.println("接收到请求："+request);
        return invoker.invoke(request);
    }

    @Bean
    public RpcInvoker createInvoker(@Autowired RpcResolver resolver){
        return new RpcInvoker(resolver);
    }
    @Bean
    public RpcResolver createResolver(){
        return new DemoResolver();
    }

    @Bean(name = "com.chen.service.UserService")
    public UserService createUserService(){
        return new UserServiceImpl();
    }

    @Bean(name = "com.chen.service.OrderService")
    public OrderService createOrderService(){
        return new OrderServiceImpl();
    }
}
